import CryptoJS from 'crypto-js';
import AES from 'crypto-js/aes';
import Base64 from 'crypto-js/enc-base64';
let tools = {

	Encrypt(srcs, skey, siv) {
		const key = CryptoJS.enc.Utf8.parse(skey); // 16位向量
		const iv = CryptoJS.enc.Utf8.parse(siv); // 16位向量
		const encrypted = CryptoJS.AES.encrypt(srcs, key, {
			iv: iv,
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.Pkcs7
		});
		let ciphertext = encrypted.toString()
		return ciphertext;
	},
	Decrypt(srcs, skey, siv) {
		const key = CryptoJS.enc.Utf8.parse(skey); // 16位向量
		const iv = CryptoJS.enc.Utf8.parse(siv); // 16位向量
		let ciphertext = Base64.parse(srcs);
		let decrypted = AES.decrypt({
			ciphertext
		}, key, {
			iv: iv,
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.Pkcs7
		});
		let plaintext = decrypted.toString(CryptoJS.enc.Utf8);
		return plaintext;
	}
}

export default tools;